package cn.pegasus.teamwork.pegasus.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.pegasus.teamwork.pegasus.entity.BussUcenterArea;
import cn.pegasus.teamwork.pegasus.mapper.BussUcenterAreaMapper;
import cn.pegasus.teamwork.pegasus.service.BussUcenterAreaService;
import cn.pegasus.teamwork.util.IDGenerator;
import cn.iyizhan.teamwork.base.service.impl.BaseServiceImpl;
import cn.iyizhan.teamwork.base.util.PageObject;

/**
 * @author ryan.bian
 * @version 
 */
@Service("areaService")
public class BussUcenterAreaServiceImpl extends BaseServiceImpl<BussUcenterArea, BussUcenterAreaMapper> implements BussUcenterAreaService {
	// 注入当前dao对象
    @Autowired
    private BussUcenterAreaMapper bussUcenterAreaMapper;

    public BussUcenterAreaServiceImpl() {
        setMapperClass(BussUcenterAreaMapper.class, BussUcenterArea.class);
    }

	@Override
	public List<BussUcenterArea> findList(String fid, Integer areaLevel) throws Exception {
		Map<String,Object> params = new HashMap<String,Object>();
		params.put("areaLevel", areaLevel);
		params.put("fid", fid);
		List<BussUcenterArea> list = bussUcenterAreaMapper.findAreaList(params);
		return list;
	}

	@Override
	public PageObject<BussUcenterArea> PageList(
			Map<String, Object> map) {
		map.put("sortName", "create_time");
        map.put("sortOrder", "desc");
		return super.Pagequery(map);
	}

	@Override
	public void addArea(Long id,Map<String, Object> map) {
		BussUcenterArea area = M2O(map);
		area.setCreateTime(new Date());
		area.setUpdateTime(new Date());
		area.setCreateUserBy(id);
		area.setUpdateUserBy(id);
		if(area.getAreaLevel()==1){
			area.setFid(-1);
		}
		area.setAreaCode(IDGenerator.get("18AE"));
		bussUcenterAreaMapper.insertByEntity(area);
	}

	@Override
	public void update(Long id, Map<String, Object> map) {
		BussUcenterArea area = M2O(map);
		area.setUpdateTime(new Date());
		area.setUpdateUserBy(id);
		bussUcenterAreaMapper.updateByEntity(area);
	}

	@Override
	public BussUcenterArea findAreaInfo(Long id) {
		BussUcenterArea a = new BussUcenterArea();
		a.setId(id);
		BussUcenterArea area = bussUcenterAreaMapper.selectSingle(a);
		if(null != area){
			Map<String,Object> map = new HashMap<String,Object>();
			Integer fid = area.getFid();
			String st = "";
			for(int i =0; i <4;i++){
				if(fid ==-1){
					break;
				}else{
					map.put("fid", fid);
					BussUcenterArea ae =bussUcenterAreaMapper.findAreaInfoById(map);
					st = ae.getAreaName()+ "  " + st;
					fid = ae.getFid();
				}
			}
			area.setName(st);
		}
		return area;
	}

	@Override
	public int delete(Long id) {
		//查询这个里面是否有下级，如果有不能删除
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("fid", id);
		int total = bussUcenterAreaMapper.findAreaTotal(map);
		if(total > 0){
			return 2;
		}
		//删除
		BussUcenterArea a = new BussUcenterArea();
		a.setId(id);
		bussUcenterAreaMapper.deleteByEntity(a);
		return 1;
	}

	/**
	 * 根据经纬度查询周边的小区信息
	 * @param map
	 * @return
	 * @throws Exception
	 */
	public List<BussUcenterArea> findListByLatLng(Map<String, Object> map) throws Exception {
		List<BussUcenterArea> list = bussUcenterAreaMapper.findListByLatLng(map);
		if(list != null && list.size() >0){
			return list;
		}
		return null;
	}
	
	@Override
	public String getAreaName(Integer id) {
		Map<String,Object> map = new HashMap<String,Object>();
		Integer fid = id;
		String st = "";
		for(int i =0; i <4;i++){
			if(fid ==-1){
				break;
			}else{
				map.put("fid", fid);
				BussUcenterArea ae =bussUcenterAreaMapper.findAreaInfoById(map);
				if(ae != null){
					st = ae.getAreaName()+ "  " + st;
					fid = ae.getFid();
				}
			}
		}
		return st;
	}
    
 
}
